package org.myorg.v6;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.apache.hadoop.io.Writable;

public class InetSocketAddressWriatble implements Writable{

	private static final Log LOG = LogFactory.getLog(InetSocketAddressWriatble.class);
	
	public int length = 0;
	public String addr = new String();
	public int port = 0;
	
    public InetSocketAddressWriatble() {
    	length = 0;
    	addr = new String();
    	port = 0;
    	
    }
	public InetSocketAddressWriatble(String addr, int port) {
		this.addr = addr;
		this.port = port;
		this.length = addr.length();
		
		//LOG.info("init: the length is " + length);
	}

	@Override
	public void write(DataOutput out) throws IOException {
		//LOG.info("write: the length is " + length);
				
		out.writeInt(length);
		out.write(addr.getBytes());
		out.writeInt(port);
	}

	@Override
	public void readFields(DataInput in) throws IOException {
		length = in.readInt();
		//LOG.info("the length is " + length);
		byte[] bt = new byte[length];
		
		for(int i = 0; i < length; i++) {
			bt[i] = in.readByte();
		}
		
		addr = new String(bt);
		port = in.readInt();
	}
	
	
	public static void main(String[] args) {
		InetSocketAddressWriatble s = new InetSocketAddressWriatble("172.16.18.79", 8989);
		System.out.println(s.addr);
		System.out.println(s.port);
		System.out.println(s.length);
	}

}
